/*这个程序是给FPGA输入两个字节的数据，然后读取pos位置*/
/*但是这个是函数版本，把这些都写成了基本的函数。*/
/*另外本版本是针对原作者的步进电机版本的tap=0,1,2,3*/

#include <stdio.h>  
#include <sys/io.h> 
#include <unistd.h>
#define setbit(x,y) x|=(1<<y)
#define clrbit(x,y) x&=~(1<<y)
int BASE=0x378;//0x378
//这个函数旨在给FPGA传送16位addvel（stepgen.c语）
void write16addvel(int w) {
	int add_reg;
	add_reg=0;//根据与fpga的约定
	outb(add_reg,BASE+3);
	outb(w & 0xff, BASE+4);
	add_reg=1;//根据与fpga的约定
	outb(add_reg,BASE+3);
    outb(w >> 8, BASE+4);
}
//这个函数旨在传递steplen_ticks,dirtime_ticks,还有tap
void write16ticks(int w){
	int add_reg;
	add_reg=6;//根据与fpga的约定
	outb(add_reg,BASE+3);
	outb(w & 0xff, BASE+4);
	add_reg=7;//根据与fpga的约定
	outb(add_reg,BASE+3);
    outb(w >> 8, BASE+4);
}
int read32pos(void) {
	int add_reg;
    unsigned char a, b, c, d;

	add_reg=0x00;
	outb(add_reg,BASE+3);
	a=inb(BASE+4);

	add_reg=0x01;
	outb(add_reg,BASE+3);
	b=inb(BASE+4);

	add_reg=0x02;
	outb(add_reg,BASE+3);
	c=inb(BASE+4);

	add_reg=0x03;
	outb(add_reg,BASE+3);
	d=inb(BASE+4);

    return a + (b<<8) + (c<<16) + (d<<24);
}

/**************************************************/
int main(unsigned char data)
{
unsigned char x;
int a,b,c,d,y;
int statusport = BASE + 1;
int controlport = BASE + 2;
unsigned char status,control;
unsigned char ctl;
//char x;
iopl(3);
ioperm(BASE,0x4ff,1);          
unsigned char EPP_ECR = 0x80;//0x80epp
unsigned char old_ECR = inb_p(BASE + 0x402);/* Paused IO for example */
unsigned char low_bits = old_ECR & 0x1f; /* save 5 lowest bits */ 
unsigned char new_ECR = EPP_ECR | low_bits;
outb_p(new_ECR, BASE + 0x402); /* set the port to EPP */


ctl = inb(controlport);
ctl = (ctl &= 0xF0) | 0x4;
outb(ctl, controlport);

ctl= inb(controlport);
setbit(ctl,5);//setbit(ctl,5)是正确的
outb(ctl,controlport);


if ((inb(statusport) & 0x01)) {//if the bit0 ==1,means TIME OUT
  status = inb(statusport); 
  outb(status | 0x01, statusport); /* Some reset by writing 1 */ 
  outb(status & 0xfe, statusport); /* Others by writing 0 */
}
//define vel0
while(1){
	write16addvel(0x400);
	write16ticks(0x0101);
/////////////////////////////
printf("pos0=%x\n",read32pos());

usleep(1000);
}

x=inb(BASE+4);
printf("x=%x\n",x);

ioperm(BASE,0x4ff,0);  
return 0;
}  
